﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmHideFieldsSettings : Form
    {
        bool bCancel = false;

        string strSettings = string.Empty;

        UserInfo ui;

        public frmHideFieldsSettings(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
        }

        private void iniFieldsList()
        {
            string[] strSelectedFields = ui.GetUserSetting("字段设置").Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
            lstFields.Items.Clear();
            if (strSelectedFields.Length > 0)
            {
                lstFields.Items.AddRange(strSelectedFields);
            }
            string[] strHideFields = ui.GetUserSetting("隐藏字段设置").Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
            lstHideFields.Items.Clear();
            if (strHideFields.Length > 0)
            {
                lstHideFields.Items.AddRange(strHideFields);
            }
            if (lstHideFields.Items.Count > 0&&lstFields .Items.Count >0)
            {
                for (int i = 0; i < lstFields.Items.Count; i++)
                {
                    if (lstHideFields.Items.IndexOf(lstFields.Items[i]) >= 0)
                    {
                        lstFields.Items[i] = "*" + lstFields.Items[i].ToString();
                    }
                }
            }
        }

        private void lstFields_DoubleClick(object sender, EventArgs e)
        {
            if (lstFields.SelectedItem.ToString().StartsWith("*"))
            {
                return;
            }
            lstHideFields.Items.Add(lstFields.SelectedItem);
            lstFields.Items[lstFields.SelectedIndex] = "*" + lstFields.SelectedItem.ToString();
        }

        private void lstHideFields_DoubleClick(object sender, EventArgs e)
        {
            object tmpobj = lstHideFields.SelectedItem;
            lstHideFields.Items.Remove(tmpobj);
            if (lstFields.Items.IndexOf("*" + tmpobj.ToString()) >= 0)
            {
                lstFields.Items[lstFields.Items.IndexOf("*" + tmpobj.ToString())] = tmpobj;
            }
        }

        public static DialogResult Show(ref string strTmpSettings, UserInfo tmpui)
        {
            frmHideFieldsSettings newFrmHideFieldsSettings = new frmHideFieldsSettings(tmpui);
            newFrmHideFieldsSettings.StartPosition = FormStartPosition.CenterParent;
            newFrmHideFieldsSettings.ShowDialog();
            if (newFrmHideFieldsSettings.bCancel)
            {
                return DialogResult.Cancel;
            }
            else
            {
                strTmpSettings = newFrmHideFieldsSettings.strSettings;
                return DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bCancel = true;
            this.Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            strSettings = string.Empty;
            for (int i = 0; i < lstHideFields.Items.Count; i++)
            {
                strSettings += lstHideFields.Items[i].ToString();
                if (i < lstHideFields.Items.Count - 1)
                {
                    strSettings += "|";
                }
            }
            this.Close();
        }

        private void frmHideFieldsSettings_Load(object sender, EventArgs e)
        {
            iniFieldsList();
        }
    }
}